package com.gitee.qdbp.general.common.biz.sms.utils;

import com.gitee.qdbp.able.beans.Duration;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.tools.cache.ICacheService;

/**
 * 最后发送时间缓存
 *
 * @author zhaohuihua
 * @version 160831
 */
public class LastTimeCache {

    private static String SMS_LAST_SEND_TIME = "SMS_LAST_SEND_TIME";

    private Duration expired;

    private ICacheService cache;

    public Long get(String phone, String type) throws ServiceException {
        return cache.get(SMS_LAST_SEND_TIME, getKey(phone, type), Long.class);
    }

    public void set(String phone, String type, Long time) throws ServiceException {
        cache.set(SMS_LAST_SEND_TIME, getKey(phone, type), time);
        if (expired != null) {
            cache.expire(SMS_LAST_SEND_TIME, getKey(phone, type), expired);
        }
    }

    protected String getKey(String phone, String type) {
        return phone + ":" + type;
    }

    public void setExpiredTime(Duration time) {
        this.expired = time;
    }

    public Duration getExpiredTime() {
        return expired;
    }

    public ICacheService getCache() {
        return cache;
    }

    public void setCache(ICacheService cache) {
        this.cache = cache;
    }

}
